<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
cfc('core')->checklogin();
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return=array();
$return['diy']=$this->plugin_setting('diy');
$basic=$this->plugin_setting('basic');
$return['basic']=$basic;
 $worker=pdo_get('jy_gohome_worker',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
 	if(!$worker && $basic['ruzhu']==1){
		
		$_RQ['op']='ruzhu';
			//待审核
	}
	
		if($worker && $worker['examine']==0 && $basic['ruzhu']==1){
			$_RQ['op']='check';
		}

if($_RQ['op']=='main'){
    $worker=pdo_get('jy_gohome_worker',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	if(!$worker){
		exi('找不到员工信息！','error','jy_gohome/store/');
	}
	if($worker['examine']==0){
		exi('您的入驻申请还在审核中，请耐心等待！','error','jy_gohome/store/');
	}
	if($worker['islock']==1){
		exi('您已被锁定，请联系管理员！','error','jy_gohome/store/');
	}
	//今日订单数量
	$day=date("Y-m-d",time("day"));
	$today_num1=pdo_count('jy_gohome_order',array('uniacid'=>$_SESSION['uniacid'],'worker_id'=>$worker['id'],'day'=>$day,'status'=>'1'));
	$today_num2=pdo_count('jy_gohome_order',array('uniacid'=>$_SESSION['uniacid'],'worker_id'=>$worker['id'],'day'=>$day,'status'=>'2'));
	$today_num3=pdo_count('jy_gohome_order',array('uniacid'=>$_SESSION['uniacid'],'worker_id'=>$worker['id'],'day'=>$day,'status'=>'4'));
	//总订单数量
	$all_num1=pdo_count('jy_gohome_order',array('uniacid'=>$_SESSION['uniacid'],'worker_id'=>$worker['id'],'status'=>'1'));
	$all_num2=pdo_count('jy_gohome_order',array('uniacid'=>$_SESSION['uniacid'],'worker_id'=>$worker['id'],'status'=>'2'));
	$all_num3=pdo_count('jy_gohome_order',array('uniacid'=>$_SESSION['uniacid'],'worker_id'=>$worker['id'],'status'=>'4'));
	//总业绩
	$sql1 = "SELECT SUM(pay) FROM " . tablename('jy_gohome_order') . " WHERE uniacid=:uniacid AND worker_id=:worker_id AND status=4";
    $all_money1 = pdo_fetch($sql1,array(":uniacid" => $_SESSION['uniacid'],":worker_id" => $worker['id']));
	$sql2 = "SELECT SUM(pay) FROM " . tablename('jy_gohome_order') . " WHERE uniacid=:uniacid AND worker_id=:worker_id AND status=2";
    $all_money2 = pdo_fetch($sql2,array(":uniacid" => $_SESSION['uniacid'],":worker_id" => $worker['id']));
	//今日业绩
	$sql3 = "SELECT SUM(pay) FROM " . tablename('jy_gohome_order') . " WHERE uniacid=:uniacid AND worker_id=:worker_id AND day=:day AND status=4";
    $today_money1 = pdo_fetch($sql3,array(":uniacid" => $_SESSION['uniacid'],":worker_id" => $worker['id'],":day" => $day));
	$sql4 = "SELECT SUM(pay) FROM " . tablename('jy_gohome_order') . " WHERE uniacid=:uniacid AND worker_id=:worker_id AND day=:day AND status=2";
    $today_money2 = pdo_fetch($sql4,array(":uniacid" => $_SESSION['uniacid'],":worker_id" => $worker['id'],":day" => $day));
	//佣金
	
	$return['worker']=$worker;
	$return['today_num']=$today_num1+$today_num2+$today_num3;
	$return['all_num']=$all_num1+$all_num2+$all_num3;
	$return['all_money']=$all_money1['SUM(pay)']+$all_money2['SUM(pay)'];
	$return['today_money']=$today_money1['SUM(pay)']+$today_money2['SUM(pay)'];
	exi($return);
}
//修改员工信息
if($_RQ['op']=='set'){
	if($_RQ['postdata']){
		$postdata=get_postdata();
	
		$data=array(
				'headimg'=>$postdata['headimg'],
				'name'=>$postdata['name'],
				'address'=>$postdata['address'],
				'phone'=>$postdata['mobile'],
				);
		$worker_id=pdo_getcolumn('jy_gohome_worker',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'id');		
		if($worker_id){
			pdo_update('jy_gohome_worker',$data,array('id'=>$worker_id));
			exi('修改成功','','jy_gohome/worker_home/');		 
		}
	}
	$worker=pdo_get('jy_gohome_worker',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	$return['worker']=$worker;
	exi($return);
	}
	
//员工入驻
if($_RQ['op']=='ruzhu'){
	if($_RQ['postdata']){
		$postdata=get_postdata();
		$data=array(
				'mid'=>$_SESSION['uid'],
				'uniacid'=>$_SESSION['uniacid'],
				'name'=>$postdata['name'],
				'phone'=>$postdata['mobile'],
				'headimg'=>$postdata['headimg'],
				'address'=>$postdata['address'],
				'age'=>$postdata['age'],
				'sex'=>$postdata['sex'],
				'lat_lng'=>ijson_encode($postdata['lat_lng']),
				'years'=>$postdata['years'],
				'remark'=>$postdata['remark'],
				'zhengjian'=>$postdata['zhengjian'],
				'status'=>1,
				'examine'=>0,//待审核
				'createtime'=>TIMESTAMP,
				);
		$worker_id=pdo_getcolumn('jy_gohome_worker',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'id');		
		if(!$worker_id){
			pdo_insert('jy_gohome_worker',$data);
			exi('成功提交申请！','','jy_gohome/worker_home/');		 
		}
		else{
			pdo_update('jy_gohome_worker',$data,array('id'=>$worker_id,'uniacid'=>$_SESSION['uniacid']));
			exi('成功提交申请！','','jy_gohome/worker_home/');
		}
	}
	$return['worker']=array();
	$return['sex']=array('女','男');
	exi($return);
	}
//审核中
if($_RQ['op']=='check'){
	$basic==$this->plugin_setting('basic');
	$return['banner']=$basic['image'];
	exi($return);
	}